import { PrismaClient } from '@prisma/client';
import bcrypt from 'bcryptjs';

const prisma = new PrismaClient();

async function main() {
  // 初始化两个测试用户
  const password = '123456';
  const hash = await bcrypt.hash(password, 10);

  const user1 = await prisma.user.upsert({
    where: { username: 'user1' },
    update: {},
    create: {
      username: 'user1',
      password: hash,
      nickname: '用户一',
      avatar: null,
      isOnline: false,
    },
  });

  const user2 = await prisma.user.upsert({
    where: { username: 'user2' },
    update: {},
    create: {
      username: 'user2',
      password: hash,
      nickname: '用户二',
      avatar: null,
      isOnline: false,
    },
  });

  // 可选：初始化一条消息
  await prisma.message.create({
    data: {
      content: '你好！这是一条测试消息。',
      type: 'text',
      senderId: user1.id,
      receiverId: user2.id,
      isRead: false,
    },
  });

  console.log('Seed completed:', { user1: user1.username, user2: user2.username });
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });